//
//  LocationTools.m
//  HiAugmentedWorld
//
//  Created by Jonathan Saggau on 4/12/10.
//  Copyright 2010 Sounds Broken inc. All rights reserved.
//

#import "LocationTools.h"


@implementation LocationTools

+(NSArray *)locations;
{
	static NSArray *locations;
	if (locations == nil) {
		NSString *locationsPath = [[NSBundle mainBundle] pathForResource:@"locations" ofType:@"plist"];
		NSArray *bundleLocationInfo = [NSArray arrayWithContentsOfFile:locationsPath];
		NSMutableArray *tempLocations = [NSMutableArray arrayWithCapacity:[bundleLocationInfo count]];
		
		for (NSDictionary *currentBundleLocation in bundleLocationInfo) {
			CLLocationCoordinate2D coord;
			coord.latitude = [[currentBundleLocation objectForKey:@"lat"] doubleValue];
			coord.longitude = [[currentBundleLocation objectForKey:@"long"] doubleValue];
			CLLocation *currentLocation = [[CLLocation alloc] initWithCoordinate:coord
																		altitude:[[currentBundleLocation objectForKey:@"alt"] doubleValue]
															  horizontalAccuracy:50
																verticalAccuracy:50
																	   timestamp:[NSDate date]];
			NSDictionary *currentLocationDict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[currentBundleLocation objectForKey:@"name"], [currentBundleLocation objectForKey:@"image"], currentLocation, nil]
																		forKeys:[NSArray arrayWithObjects:@"name", @"image", @"location", nil]];
			[currentLocation release];
			[tempLocations addObject:currentLocationDict];
		}
		locations = [tempLocations copy];
	}
	return locations;
}

+(float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {
	float longitudinalDifference = second.longitude - first.longitude;
	float latitudinalDifference = second.latitude - first.latitude;
	float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
	if (longitudinalDifference > 0) return possibleAzimuth;
	else if (longitudinalDifference < 0) return possibleAzimuth + M_PI;
	else if (latitudinalDifference < 0) return M_PI;
	
	return 0.0f;
}

@end
